# -*- coding:utf-8 -*-
import cv2 as cv
import sys
import numpy as np

def usm_sharpening(image, sigma=0.7, strength=10):
    # 高斯模糊
    blurred = cv.GaussianBlur(image, (0, 0), sigma)
    # 锐化图像 = 原始图像 + 原始图像 - 模糊后的图像
    sharpened = cv.addWeighted(image, 1.0 + strength, blurred, -strength, 0)
    return sharpened


if __name__ == '__main__':
    # 迭代次数
    cycleNum = 1


    # 读取图像face1.png和face2.png
    # fileName = '8'
    # image = cv.imread('diedai/' + fileName + '.jpg', cv.IMREAD_ANYCOLOR)

    fileName = r"E:\infraVideos\20240424\texture\phase.jpg"
    image = cv.imread(fileName, cv.IMREAD_ANYCOLOR)
    # 展示结果
    cv.imshow('Origin_image', image)

    # 对双边滤波加锐化算法迭代
    for i in range(cycleNum):
        # 双边滤波
        res = cv.bilateralFilter(image, 30, 1000, 500)
        # USM 锐化
        sharpened_image = np.zeros_like(res)
        if sharpened_image.ndim == 3: # 判断图片是三维还是二维
            for i in range(3):  # 对每个通道进行处理
                sharpened_image[:, :, i] = usm_sharpening(res[:, :, i])
        else:
            sharpened_image[:, :] = usm_sharpening(res[:, :])
        # 用锐化后的图像继续下一轮迭代
        image = sharpened_image

    # # 单独进行锐化迭代
    # for i in range(1):
    #     # USM 锐化
    #     sharpened_image = np.zeros_like(image)
    #     for i in range(3):  # 对每个通道进行处理
    #         sharpened_image[:, :, i] = usm_sharpening(image[:, :, i])
    #
    #     # 用锐化后的图像继续下一轮迭代
    #     image = sharpened_image

    # 展示结果
    cv.imshow('final_image', image)
    cv.imwrite(r"E:\infraVideos\20240424\texture\phaseFilter.jpg", image)
    # cv.imwrite('dieDai/'+fileName+'dieDai'+str(cycleNum)+'.jpg', image)
    cv.waitKey(0)
    cv.destroyAllWindows()